/**
 *  mailbox.h - Related routines and structs of mailbox.
 *
 *  Copyright (C) 2008-2009 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef __MAILBOX_H__
#define __MAILBOX_H__

#include "include/types.h"

#ifndef UNIT_DEBUG
#include "include/mem.h"
#include "include/irq.h"
#include "include/core.h"
#endif


/* struct message - It describes the style of message */
typedef struct message {
    uword_t key_id;     /* Message ID that identify a message */
    word_t send_id;    /* ID of sending task */
    word_t rev_id;     /* ID of receiving task */
    void *pmsg;         /* Pointer to the address of message */
} msg_t;

/* mailbox - It is prototype of mailbox */
typedef struct mailbox {
    uword_t size;       /* The size of mailbox */
    uword_t used;       /* The number of used space of mailbox */
    msg_t *start;       /* Pointer to the start address of mailbox */
    msg_t *end;         /* Pointer to the end address of mailbox */
    msg_t *pos;         /* current write-read postion of mailbox */
} mail_t;

/* Created a new mailbox */
mail_t *mboxInit(uword_t Size);

/* Sending a message to a mailbox */
bool_t mboxSend(mail_t *pMail,/* Pointer to the mailbox. */
                 uword_t Key,  /* Serial number of message. */
                 word_t RevID,/* ID of Receiving task. */
                 void *pMsg);

/* Retrieve message from mailbox */
void *mboxRev(mail_t *pMail,   /* Pointer to the mailbox. */
              uword_t Key,     /* Serial number of message. */
              word_t SendID);  /* ID of sending task. */

/* Get current used space of mailbox. */
word_t mboxGetUsed(const mail_t *pMail);

/* Clear a mailbox */
void mboxClear(mail_t *pMail);

/* Delete a mailbox */
void mboxDel(mail_t *pMail);


#endif

